package ar.com.ctrln.view.dashboard.mantenimiento.ticket;

import java.awt.Desktop;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.event.EventQueue;
import org.zkoss.zk.ui.event.EventQueues;
import org.zkoss.zk.ui.select.annotation.Listen;
import org.zkoss.zk.ui.select.annotation.VariableResolver;
import org.zkoss.zk.ui.select.annotation.Wire;
import org.zkoss.zk.ui.select.annotation.WireVariable;
import org.zkoss.zk.ui.util.Clients;
import org.zkoss.zul.Button;
import org.zkoss.zul.Checkbox;
import org.zkoss.zul.Combobox;
import org.zkoss.zul.Datebox;
import org.zkoss.zul.Label;
import org.zkoss.zul.ListModelList;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Popup;
import org.zkoss.zul.Row;
import org.zkoss.zul.Textbox;
import org.zkoss.zul.Timebox;
import org.zkoss.zul.Timer;
import org.zkoss.zul.Window;

import ar.com.ctrln.control.mantenimiento.ticket.RegistrarActividad;
import ar.com.ctrln.persistence.model.cliente.Cliente;
import ar.com.ctrln.persistence.model.insumo.Insumo;
import ar.com.ctrln.persistence.model.ticket.EstadoTicket;
import ar.com.ctrln.persistence.model.ticket.Ticket;
import ar.com.ctrln.persistence.model.ticket.actividad.Actividad;
import ar.com.ctrln.persistence.model.ticket.actividad.DetalleActividad;
import ar.com.ctrln.persistence.model.vehiculo.Vehiculo;
import ar.com.ctrln.view.render.compras.insumo.InsumoItemRender;
import ar.com.ctrln.view.render.mantenimiento.actividad.DetActividadItemRender;
import ar.com.ctrln.view.ui.comportamientoVentana.AbstractMinimizarVentana;

@SuppressWarnings("unused")
@VariableResolver(org.zkoss.zkplus.spring.DelegatingVariableResolver.class)
public class RegistrarDetalleActividadController extends AbstractMinimizarVentana {

	private static final long serialVersionUID = 7293861791737040132L;

	@WireVariable
	private RegistrarActividad registrarActividad;
	
	private Actividad actividad;
	
	//private EventQueue eq2;
	
	private EventQueue eq = EventQueues.lookup("descontarInsumos", EventQueues.DESKTOP, true);

	private EventQueue eq1 = EventQueues.lookup("devolverInsumos", EventQueues.DESKTOP, true);
	
	private EventQueue eq2 = EventQueues.lookup("closeInsumos", EventQueues.DESKTOP, true);

	@Wire
	private Window wRegistrarDetalleAct;
			
	@Wire
	private Listbox lbConsultarInsumos;
	
	@Wire
	private Button btnDetCancelar;
	
	private ListModelList listaInsumos;
	
		
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public void doAfterCompose(Component comp) throws Exception {
		super.doAfterCompose(comp);
		Clients.showNotification("Arrastre los Insumos que desea asignar a la Actividad",Clients.NOTIFICATION_TYPE_INFO, lbConsultarInsumos, "before_center", 6000);

		if(Executions.getCurrent().getSession().getAttribute("ListaActual")!=null){
			listaInsumos = (ListModelList) Executions.getCurrent().getSession().getAttribute("ListaActual");
		}
		else{
			listaInsumos = new ListModelList(registrarActividad.listaInsumos());
		}
		lbConsultarInsumos.setModel(listaInsumos);

		lbConsultarInsumos.setItemRenderer(new InsumoItemRender());
		
		eq.subscribe(new EventListener<Event>() {
		    public void onEvent(Event event) throws Exception {
		    	    DetalleActividad det = (DetalleActividad)event.getData();	
		    	    Insumo ins = det.getInsumo();
		    	    ins.setStock(ins.getStock() - det.getCantidad());
		    	    lbConsultarInsumos.setModel(listaInsumos);
		    }
		});

		eq1.subscribe(new EventListener<Event>() {
		    public void onEvent(Event event) throws Exception {
		    	    DetalleActividad det = (DetalleActividad)event.getData();	
		    	    Insumo ins = det.getInsumo();
		    	    ins.setStock(ins.getStock() + det.getCantidad());
		    	    lbConsultarInsumos.setModel(listaInsumos);
     	    }
		});
		
		eq2.subscribe(new EventListener<Event>() {
		    public void onEvent(Event event) throws Exception {
		    	salirDetActividad();
		    }
		});
		
	}
	@Listen("onClick = #btnDetCancelar")
	public void salirDetActividad(){
		
		eq.close();
		
		eq1.close();
		eq2.close();
		Executions.getCurrent().getSession().setAttribute("ListaActual", listaInsumos);
		wRegistrarDetalleAct.onClose();
	}
	
	
	
}

